<?php namespace App\Http\Controllers;

use App\Admin;
use App\Banner;

use App\Http\Services\FileManager;

use Blocktrail\CryptoJSAES\CryptoJSAES;

use App\User;
use App\UserInfo;

use Illuminate\Contracts\Auth\Guard;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function bannerList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Banner $sysBanner */
        $sysBanners = Banner::orderBy('sort','asc')->orderBy('created_at','desc')->get();
        if(count($sysBanners) > 0){
            foreach($sysBanners as $sysBanner){
                $sysBanner->img_url = FileManager::getImgUrl($sysBanner->img_id);
            }
        }

        return view('admin.banner-list',['banners'=>$sysBanners]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();
        if($thatBanner){
            $thatBanner->img_url = FileManager::getImgUrl($thatBanner->img_id);
        }

        return view('admin.banner-set',['banner'=>$thatBanner]);
    }

    public function userList(Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $users = User::orderBy("created_at", "desc")->paginate(50);
        if (count($users) > 0){
            foreach ($users as $user) {
                /** @var User $user */
                $user->userInfo;
                if ($user->userInfo) {
                    $user->userInfo->photo_url = FileManager::getImgUrl($user->userInfo->photo_id);
                }
            }
        }

        return view('admin.user-list',["users"=>$users]);
    }
    public function addDescription(Request $request)
    {
        $user_id=!is_null($request->get('user_id'))?$request->get('user_id'):'';
        $description=$request->get('description');
        if($user_id !='')
        {
            $userInfo=UserInfo::whereId($user_id)->first();
            if($description !='')
            {
                $userInfo->description=$description;
                if($userInfo->save())
                    return response()->json(['level'=>'success','message'=>'备注成功']);
            }
            else
            {
                $userInfo->description=null;
                $userInfo->save();
                return response()->json(['level'=>'success','message'=>'备注已清除！']);
            }
        }
        return response()->json(['level'=>'error','message'=>'操作失败']);

    }

    public function adminList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Admin $newAdmin */
        $newAdmins = Admin::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.admin-list', ['admins' => $newAdmins]);
    }

    public function adminSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.admin-set');
    }

    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.change-psw');
    }

    public function classifyList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify = Classify::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.classify-list',['classify' => $classify]);
    }


}